package com.yugao.fintech.antelope.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.shop.assembly.SkuStockAssembly;
import com.yugao.fintech.antelope.shop.manager.SkuStockManager;
import com.yugao.fintech.antelope.shop.mapper.SkuStockMapper;
import com.yugao.fintech.antelope.shop.model.entity.SkuStock;
import com.yugao.fintech.antelope.shop.model.rqrs.sku.SkuPageReq;
import com.yugao.fintech.antelope.shop.model.rqrs.sku.SkuReq;
import com.yugao.fintech.antelope.shop.model.rqrs.sku.SkuResp;
import com.yugao.fintech.antelope.shop.service.SkuStockService;
import com.yugao.fintech.framework.mybatisplus.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 商品sku
 *

 */
@Slf4j
@Service
public class SkuStockServiceImpl extends ServiceImpl<SkuStockMapper, SkuStock>
    implements IService<SkuStock>, SkuStockService {

    @Autowired
    private SkuStockManager skuManager;
    @Autowired
    private SkuStockAssembly skuAssembly;
    
    @Override
    public IPage<SkuResp> pageSku(SkuPageReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<SkuStock> lqw = SkuStock.lqw()
            .eq(StringUtils.isNotEmpty(req.getSkuCode()), SkuStock::getSkuCode, req.getSkuCode())
            .ge(Objects.nonNull(beginTime), SkuStock::getCreatedAt, beginTime)
            .le(Objects.nonNull(endTime), SkuStock::getCreatedAt, endTime)
            .orderByDesc(SkuStock::getCreatedAt);
        IPage<SkuStock> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, skuAssembly::toSkuResp);
    }

    @Override
    public SkuResp getSku(Long id) {
        SkuStock entity = this.getById(id);
    	return skuAssembly.toSkuResp(entity);
    }

    @Override
    public boolean saveSku(SkuReq req) {
        SkuStock entity = skuAssembly.toSku(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateSku(SkuReq req) {
        SkuStock entity = skuAssembly.toSku(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteSku(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteSku(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
